package com.zhiyume.service;

import com.zhiyume.service.common.ConstantEnum;
import com.zhiyume.util.RedisClient;
import com.zhiyume.service.util.StringUtil;
import com.zhiyume.util.TypeConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Random;

/**
 * Created by wang on 17/11/5.
 */
@Service
public class RedisService {
    private static final String MODULE= RedisService.class.getName();
    private static final Logger logger = LoggerFactory.getLogger(RedisService.class);

    @Autowired
    private RedisClient redisClient;

    public boolean setWXTOKENData(String value,int expires_in){
        try{
            redisClient.setx(TypeConstants.WXConstants.WX_TOKEN_KEY,value,expires_in);
        }catch (Exception e){

        }
        return false;
    }

    public String getWXTOKENData(){
        return getData(TypeConstants.WXConstants.WX_TOKEN_KEY);
    }

    public boolean setWXTICKETData(String value,int expires_in){
        try{
            redisClient.setx(TypeConstants.WXConstants.WX_TICKET_KEY,value,expires_in);
        }catch (Exception e){

        }
        return false;
    }

    public String getWXTICKETData(){
        return getData(TypeConstants.WXConstants.WX_TICKET_KEY);
    }

    public String getData(String key){
        try{
            if(StringUtil.isNotBlank(key)){
                String value = redisClient.get(key);
                if(StringUtil.isNotBlank(value)) return value;
            }
        }catch (Exception e){
            logger.error(e.getMessage());
        }
        return "";
    }

    public Long getUserId(String token){
        Long uid = -1L;
        try{
            if(StringUtil.isNotBlank(token)){
                String value = redisClient.get(ConstantEnum.CONSTANT_TOKEN_PRE.getErrType() + token);
                if(StringUtil.isNotBlank(value)) uid= Long.valueOf(value);
            }
        }catch (Exception e){
            logger.error(e.getMessage());
        }
        return uid;
    }

    public Long userRead(String token){
        return userRead(getUserId(token));
    }

    public Long userRead(Long uid) {
        if(uid>-1){
            //用户阅读量id
            try {
                return redisClient.incr(TypeConstants.REDIS_USER_KEY+uid);
            } catch (Exception e) {
                logger.error(e.getMessage());
            }
        }
        return 0L;
    }

    public Long informationRead(Long id){
        //稿件被阅读量

        return theKeyRead(id,TypeConstants.REDIS_INFORMATION_KEY);
    }


    public Long informationHaveRead(Long id){
        //稿件被阅读量
        return theKeyHaveRead(id,TypeConstants.REDIS_INFORMATION_KEY);
    }


    public Long activityRead(Long id){
        //活动被阅读量

        try {
            String o = redisClient.get(TypeConstants.REDIS_ACTIVITY_KEY+id);
            if(StringUtil.isNotBlank(o)){
                return redisClient.incr(TypeConstants.REDIS_ACTIVITY_KEY+id);
            }

        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return 1L;
    }


    public Long activityHaveRead(Long id){
        //活动被阅读量
        try {
            String o = redisClient.get(TypeConstants.REDIS_ACTIVITY_KEY+id);
            if(StringUtil.isNotBlank(o)) {
                return Long.valueOf(o);
            }

            redisClient.set(TypeConstants.REDIS_ACTIVITY_KEY+id,"1");
        } catch (Exception e) {
            logger.error(e.getMessage());
        }

        return 1L;
    }


    private Long theKeyRead(Long id,String key){
        //稿件被阅读量

        try {
            String o = redisClient.get(key+id);
            if(StringUtil.isNotBlank(o)){
                if(Long.valueOf(o)>=TypeConstants.InformationConstants.MIN_READ){
                    return redisClient.incr(TypeConstants.REDIS_INFORMATION_KEY+id);
                }
            }

            Long randomRead = TypeConstants.InformationConstants.MIN_READ+new Random().nextInt(999);
            redisClient.set(TypeConstants.REDIS_INFORMATION_KEY+id, randomRead.toString());
            return randomRead;

        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return 0L;
    }

    private Long theKeyHaveRead(Long id,String key){
        //稿件被阅读量
        try {
            String o = redisClient.get(key+id);
            if(StringUtil.isNotBlank(o)) {
                return Long.valueOf(o);
            }else{
                return informationRead(id);
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return Long.valueOf(String.valueOf(new Random().nextInt(100)));
    }
}
